<?php

namespace backend\controllers;

use Yii;
use backend\models\GrpShopPosition;
use backend\models\ShopPositionSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\GrpBrand;
use common\models\Area;
use common\models\City;
use yii\web\ForbiddenHttpException;

/**
 * ShopPositionController implements the CRUD actions for GrpShopPosition model.
 */
class ShopPositionController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    public function beforeAction($action)
    {
    	//如果未登录，则直接跳转
    	if(Yii::$app->user->isGuest){
    		return $this->redirect(['/site/login']);
    	}
    
    
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	if(\Yii::$app->user->can('shop-position-manage')){
    		return true;
    	}else{
    		throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
    	}
    }
    /**
     * Lists all GrpShopPosition models.
     * @return mixed
     */
    public function actionIndex()
    {
    	$model_brand = GrpBrand::find()->all();
    	//var_dump($model_brand);
        $searchModel = new ShopPositionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        	'model_brand' => $model_brand,
        ]);
    }

    /**
     * Displays a single GrpShopPosition model.
     * @param integer $id
     * @param string $longitude
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new GrpShopPosition model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new GrpShopPosition();
		$model_brand = GrpBrand::find()->all();
		//var_dump($model_brand); 
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
            	'model_brand'=>$model_brand,
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing GrpShopPosition model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @param string $longitude
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $province_id = City::findOne(['name' => $model->province])->id;
        $_province_id = substr((string)$province_id, 0, 2);
        $city = City::findBySql("select * from grp_city where id like '$_province_id%00' and id <>$province_id")->all();
        
        $arr_city = array('北京市','上海市','天津市','重庆市');
        $province_name = $model->province;
    	if(in_array($province_name,$arr_city)){
		$city = City::findBySql("select * from grp_city where name='$province_name'")->all();
    	}
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            	'city' => $city
            ]);
        }
    }

    /**
     * Deletes an existing GrpShopPosition model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @param string $longitude
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the GrpShopPosition model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @param string $longitude
     * @return GrpShopPosition the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = GrpShopPosition::findOne(['id' => $id])) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionSelectprovince($province_name){
    	$province_id = City::findOne(['name' => $province_name])->id;
    	$_province_id = substr((string)$province_id, 0, 2);
    	
    	//$result = Area::findAll(['area_pid'=>$province_id]);
    	$result = City::findBySql("select * from grp_city where id like '$_province_id%00' and id <>$province_id")->all();
    	$option = '';
    	
    	if ($result) {
    		foreach ($result as $value) {
    			$option .= '<option value="' . $value['name'] . '">' . $value['name'] . '</option>';
    		}
    	} else {
    		$option .= '<option value="0">暂无</option>';
    	}
    	$arr_city = array('北京市','上海市','天津市','重庆市');
    	if(in_array($province_name,$arr_city)){
    		$option = '<option value="' . $province_name . '">' . $province_name . '</option>';
    	}
    	echo $option;
    	
    }
    /*选择品牌*/
    public function actionSelectBrand($head_grp){
    	$model = GrpBrand::findAll(['head_grp'=>$head_grp]);
    	$selece_brand ='';
    	foreach ($model as $value){
    		//$selece_brand .= '<label id="'.$value['id'].'"><input name="GrpShopPosition[brand_id]" value="'.$value['id'].'" type="checkbox" ></input>'.$value['name'].'</label>';
    		$selece_brand .= '<label id="'.$value['id'].'"><input name="GrpShopPosition[brand_id]" value="'.$value['id'].'" type="radio" ></input>'.$value['name'].'</label>';
    	}
    	echo $selece_brand;
    
    }
    public function actionDeleteBrand($head_grp){
    	$model = GrpBrand::findAll(['head_grp'=>$head_grp]);
    	$arr = array();
    	foreach ($model as $value)
    	{
    		array_push($arr, $value['id']);
    	}
    	//var_dump($arr);
    	return json_encode($arr);
    }
    /*筛选使用
     * */
    public function actionSelectBrandForSearch($head_grp){
    	$model = GrpBrand::findAll(['head_grp'=>$head_grp]);
    	$selece_brand ='';
    	foreach ($model as $value){
    		$selece_brand .= '<label id="'.$value['id'].'"><input name="ShopPositionSearch[brand_id]" value="'.$value['id'].'" type="checkbox" ></input>'.$value['name'].'</label>';
    	}
    	echo $selece_brand;  
    }
}
